﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Globalization;
using Public;

namespace WebsiteAdmin.Wilson.ProInfo.Handler
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ProInsert : IHttpHandler
    {
        private int proReNum = 0;
        private DateTime startDate;
        private DateTime endDate;
        Model.Product pro = new Model.Product();
        private string strCon = Global.ConnWilson;
        private string responseText = string.Empty;

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Cookie.AdminLoginName != null && Cookie.AdminLoginName != "")
            {
                InitData(context); //初始化数据   
                int insertResult = Insert(strCon, pro);
                if (insertResult == 1)
                {
                    int rtn = InsertRe(strCon, startDate, endDate);
                    responseText = rtn == 1 ? "{\"Done\":\"Success\"}" : "{\"Done\":\"Failed\"}";
                }
                else 
                {
                    responseText = "{\"Done\":\"Failed\"}";
                }
            }
            context.Response.Write(responseText);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }

        #region InitData
        private void InitData(HttpContext context)
        {
            pro.CatId = Convert.ToInt16(context.Request["CatID"],CultureInfo.InvariantCulture);
            pro.SerId = Convert.ToInt16(context.Request["SerID"],CultureInfo.InvariantCulture);
            pro.ProName = context.Request["ProName"];
            pro.ProTypeNum = context.Request["ProTypeNum"].ToString();
            pro.Keywords = context.Request["Keywords"];
            pro.Summary = context.Request["Summary"];
            pro.Description = context.Request["Description"];
            pro.ProCode = context.Request["ProCode"];
            pro.MainImgURL = context.Request["MainImgURL"];
            pro.MediumImgURL = context.Request["MediumImgURL"].ToString();
            pro.BigImgURL = context.Request["BigImgURL"].ToString();
            pro.ProContent = context.Request["ProContent"];
            pro.SortBy = Convert.ToInt16(context.Request["SortBy"],CultureInfo.InvariantCulture);
            pro.PackInfo = context.Request["PackInfo"].ToString();
            proReNum = Convert.ToInt16(context.Request["ProReNum"].ToString(), CultureInfo.InvariantCulture);
            startDate = Convert.ToDateTime(context.Request["StartDate"], CultureInfo.InvariantCulture);
            endDate = Convert.ToDateTime(context.Request["EndDate"], CultureInfo.InvariantCulture);
        }
        #endregion

        #region Insert
        protected int Insert(string strCon, Model.Product pro)
        {
            return Bll.Product.Insert(strCon, pro);
        }

        protected int InsertRe(string strCon,DateTime starttime,DateTime endtime) 
        {
            return Bll.RecommendPro.INSERT(strCon, starttime, endtime);
        }
        #endregion

        #region IsReusable
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        #endregion
    }
}
